package com.stylefeng.guns.modular.fileManage.service.impl;

import com.stylefeng.guns.common.persistence.dao.ZyBrewsPictureMapper;
import com.stylefeng.guns.common.persistence.model.ZyBrewsPicture;
import com.stylefeng.guns.modular.fileManage.dao.ZyBrewsPictureDao;
import com.stylefeng.guns.modular.fileManage.enums.TeaLabelEnums;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.model.ZyPicture;
import com.stylefeng.guns.common.persistence.model.ZyTeaPicture;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.fileManage.dao.ZyPictureDao;
import com.stylefeng.guns.modular.fileManage.dao.ZyTeaDao;
import com.stylefeng.guns.modular.fileManage.dao.ZyTeaPictureDao;
import com.stylefeng.guns.modular.fileManage.service.UploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.*;

/**
 * <p>
 * 图片上传、删除业务类
 * </p>
 *
 * @author ghp
 * @since 2017-12-28
 */
@Service
public class UploadServiceImpl implements UploadService, Serializable {

    @Resource
    private GunsProperties gunsProperties;

    @Resource
    private ZyPictureDao zyPictureDao;

    @Resource
    private ZyTeaPictureDao zyTeaPictureDao;

    @Resource
    private ZyBrewsPictureDao brewsPictureDao;

    /**
     * 上传图片
     *
     * @param files
     * @param teaId
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @Override
    @Transactional
    public List<ZyPicture> upload(MultipartFile[] files, Integer teaId) throws IOException, ParseException {

        List<ZyPicture> list = new ArrayList<>();

        ZyPicture zyPicture = new ZyPicture();

        ZyTeaPicture zyTeaPicture = new ZyTeaPicture();

        Logger log = LoggerFactory.getLogger(UploadServiceImpl.class);

        //图片id
        Integer pictureId;

        //保存的图片名称
        String fileNameNew;

        //图片文件存储路径
        String realPath;

        //当前登录用户的id
        Integer userId = ShiroKit.getUser().getId();
        //得到图片保存路径
        realPath = getMonthPath();

        log.debug("realPath" + realPath);
        //遍历上传文件过程
        for (MultipartFile file : files) {
            File destFile = new File(realPath);
            if (!destFile.exists()) {
                destFile.mkdirs();
            }
            //获得图片id
            pictureId = zyPictureDao.selectMaxId() + 1;

            //生成图片保存名称
            fileNameNew = pictureId + "_" + file.getOriginalFilename();
            //保存图片到目录中
            File f = new File(realPath + fileNameNew);
            file.transferTo(f);
            f.createNewFile();

            zyPicture.setId(pictureId);
            zyPicture.setOriginalName(file.getOriginalFilename());
            zyPicture.setSaveName(fileNameNew);
            zyPicture.setCrtTime(new Date());
            zyPicture.setCrtUser(Integer.toString(userId));
            zyPicture.setSavePath(realPath);

            zyPictureDao.insert(zyPicture);
            list.add(zyPicture);
            if (teaId != null) {
                zyTeaPicture.setTeaId(teaId);
                zyTeaPicture.setPictureId(pictureId);
                zyTeaPictureDao.insert(zyTeaPicture);
            }
        }
        return list;
    }

    /**
     * 删除图片
     */
    @Override
    public void delete(Integer pictureId) {
        ZyPicture zyp = zyPictureDao.selectByPictureId(pictureId);
        String filename = zyp.getSaveName();
        String path = zyp.getSavePath();
        File f = new File(path + filename);

        zyTeaPictureDao.delete(pictureId);
        brewsPictureDao.deleteByPicId(pictureId);
        zyPictureDao.delete(pictureId);
        //如果当前文件已经存在了，就删除。
        if (f.exists()) {
            f.delete();
        }
    }

    /**
     * 获得当前月份的茶叶图片目录
     */
    public String getMonthPath(){
        String monthPath = gunsProperties.getTeaPath();
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        //获取年
        int year = cal.get(Calendar.YEAR);
        //获取月份，0表示1月份
        int month = cal.get(Calendar.MONTH) + 1;
        //判断是否为个位数如果是个位数则前面加上0
        if(month < 10){
            monthPath = monthPath + year + "0" + month + "/";
        }else{
            monthPath = monthPath + year + month + "/";
        }
        return monthPath;
    }
}
